package com.example.home.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.RecyclerView
import com.example.home.databinding.SimpleItemBinding
import com.example.image.ImageLoadUtil
import com.example.image.ImageOptions
import com.example.main.ui.home.entity.SimpleVideoEntity
import kotlin.math.max

class HomeVideoListAdapter constructor(private val list:MutableList<SimpleVideoEntity>):RecyclerView.Adapter<HomeVideoListAdapter.ItemViewHolder>() {
    private var scrollState = RecyclerView.SCROLL_STATE_IDLE
    private var isPreloading = false  //是否处于预加载状态
    var preloadData : (() -> Unit)? = null  //预加载执行的方法
    var preloadEndCount = 0
    private var itemClickListener:OnItemClickListener? = null
    fun setOnItemClickListener(listener:OnItemClickListener){
        itemClickListener = listener
    }
    inner class ItemViewHolder(val dataBinding: SimpleItemBinding):RecyclerView.ViewHolder(dataBinding.root)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemViewHolder {
        val dataBinding =
            SimpleItemBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return ItemViewHolder(dataBinding)
    }

    override fun onBindViewHolder(holder: ItemViewHolder, position: Int) {
        val entity = list[position]
        if(entity != null){
            //设置标题描述
            holder.dataBinding.tvDesc.text  = entity.description
            holder.dataBinding.userName.text = entity.name

            ImageLoadUtil.with(holder.dataBinding.root.context)
                .setOptions(ImageOptions.Builder.isCircle(true).build())
                .loadUrl(entity.avatar_url)
                .into(holder.dataBinding.userHead)

            ImageLoadUtil.with(holder.dataBinding.root.context)
                .setOptions(ImageOptions.Builder.isCircle(false).build())
                .loadUrl(entity.videomainimag)
                .into(holder.dataBinding.mainImage)
            holder.dataBinding.mainImage.setOnClickListener {
                itemClickListener?.onItemClick(it,position)
            }
            holder.dataBinding.ivMore.setOnClickListener {
                itemClickListener?.onItemClick(it,position)
            }
            checkPreload(position)
        }
    }

    private fun checkPreload(position: Int) {
        if(scrollState != RecyclerView.SCROLL_STATE_IDLE
            && !isPreloading
            && position == max(list.size - 1 - preloadEndCount,0)){
            isPreloading = true
            preloadData?.invoke()
        }else{
            isPreloading = false
        }
    }

    override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
        recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener(){
            override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
                scrollState = newState
                super.onScrollStateChanged(recyclerView, newState)
            }
        })
    }

    //获取item的标识
    override fun getItemId(position: Int): Long = list[position].id.toLong()


    //如果页面是多布局，需要返回不同的值
    override fun getItemViewType(position: Int): Int {
        return 0
    }

    //获取Item数量
    override fun getItemCount(): Int  = list.size

    interface OnItemClickListener{
        fun onItemClick(v:View,position: Int)
    }

}